/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cashbillpanel.java
 *
 * Created on Oct 9, 2010, 3:46:33 PM
 */

package cat20032010;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import java.util.Date;
import java.lang.String;
/**
 *
 * @author acer
 */
public class cashbillpanel extends javax.swing.JPanel {
    Vector data = new Vector();
    Vector columnNames = new Vector();
    Statement stmt;
    private ResultSet rset;
    String dateString="";
    Date currentDate  = new Date();
    int bug = 0 ;
    double subTotal = 0;
  //  static Print Print = new Print();
    /** Creates new form cashbillpanel */
    public cashbillpanel() {
        initComponents();
        
       
    }
public void getdata()
    {
    boolean found = true;
    int tableNum = (int)this.tablebox.getSelectedIndex();
        System.out.println(tableNum);
        Vector data = new Vector();
        Vector columnNames = new Vector();
        subTotal = 0;
        try {
            Main.getConnection();
            stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

            rset = stmt.executeQuery("SELECT menu_code as menu_code, Menu_Name as Menu_Name, Menu_QTY as Quantity, total_price as Price from order_list where table_num = '"+tableNum+"' and Bill_Status = 'U' and order_date = '"+dateString+"'");
            rset.first();

            ResultSetMetaData md = rset.getMetaData();
            int columns = md.getColumnCount();

            //  Get column names
            subTotal = (double) Double.valueOf(rset.getString(4).trim()).doubleValue();
            while (rset.next()){
            	System.out.println(subTotal+"  "+rset.getString(4));
            subTotal = subTotal + (double) Double.valueOf(rset.getString(4).trim()).doubleValue();
            System.out.println(subTotal+"  "+rset.getString(4));
            }
            rset.first();
            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(md.getColumnName(i));
            }

            rset.first();
            if (rset.isFirst() == true && rset.isLast() == true) {

            }
            else if(rset.isFirst() == true) {
                // JOptionPane.showMessageDialog(null, "Hello");
                Vector row = new Vector(columns);

                for (int i = 1; i <= columns; i++) {
                    row.addElement(rset.getString(i));
                }

                data.addElement(row);
            }else{
                System.out.print("sssssaaaaaaaaaaaaa");
                found = false;
                JOptionPane.showMessageDialog(null, "No Record Found!", "MESSAGE!", JOptionPane.INFORMATION_MESSAGE);
            }


            if (rset.isFirst() == true && rset.isLast() == true) {
                // JOptionPane.showMessageDialog(null, "Hello");
                Vector row = new Vector(columns);

                for (int i = 1; i <= columns; i++) {
                    row.addElement(rset.getString(i));
                }

                data.addElement(row);
            } else {
                while (rset.next()) {
                    // rset.next();
                    Vector row = new Vector(columns);

                    for (int i = 1; i <= columns; i++) {
                        row.addElement(rset.getString(i));
                    }

                    data.addElement(row);
                }
            }
        }
            catch (SQLException e) {
                JOptionPane.showMessageDialog(null, "Data Not Found", "Error!", JOptionPane.ERROR_MESSAGE);
                System.out.println("No Data Found");
            }

        DefaultTableModel model = new DefaultTableModel(data, columnNames)
       {
        public boolean isCellEditable(int row,int column){
            return false;

        }
    };
        jTable2.setModel(model);
    bug = 0;
    DecimalFormat currency = new DecimalFormat("#0.00");
    String Sub = currency.format(subTotal);
    jTextField7.setText("10%");
    double finaltotal = subTotal * 110 / 100 ;
    String Total = currency.format(finaltotal);
    jTextField2.setText(Total);   
    
    jTextField6.setText(Sub);
    if (found ==false){
        jTable2.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null}
    },
    new String [] {
        "No.", "Menu Code", "Menu Name", "Quantity", "Price"
    }
));
    }

    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextField1 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        tablebox = new javax.swing.JComboBox();
        date = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        current_time = new javax.swing.JTextField();

        jTable1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Menu Code", "Menu Name", "Quantity", "Price"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jTextField1.setToolTipText("TOTAL PRICE");

        setBackground(new java.awt.Color(254, 249, 195));
        setPreferredSize(new java.awt.Dimension(620, 460));
        setRequestFocusEnabled(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Cash Bill", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 0, 18))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(620, 460));
        jPanel1.setLayout(null);

        jLabel2.setBackground(new java.awt.Color(153, 153, 255));
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setForeground(new java.awt.Color(0, 0, 255));
        jLabel2.setText("Table No.");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(20, 40, 80, 17);

        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jPanel1.add(jButton1);
        jButton1.setBounds(220, 340, 33, 9);

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });
        jPanel1.add(jTextField2);
        jTextField2.setBounds(440, 300, 120, 39);

        jTable2.setForeground(new java.awt.Color(255, 0, 0));
        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "No.", "Menu Code", "Menu Name", "Quantity", "Price"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Integer.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable2);

        jPanel1.add(jScrollPane2);
        jScrollPane2.setBounds(10, 120, 574, 157);

        jLabel5.setBackground(new java.awt.Color(153, 153, 255));
        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel5.setForeground(new java.awt.Color(0, 0, 204));
        jLabel5.setText("Subtotal");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(20, 300, 80, 17);

        jLabel6.setBackground(new java.awt.Color(153, 153, 255));
        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel6.setForeground(new java.awt.Color(0, 0, 204));
        jLabel6.setText("Taxes");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(20, 330, 70, 17);

        jLabel7.setBackground(new java.awt.Color(153, 153, 255));
        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel7.setForeground(new java.awt.Color(0, 0, 204));
        jLabel7.setText("Total");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(350, 310, 50, 17);

        jLabel8.setBackground(new java.awt.Color(153, 153, 255));
        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel8.setForeground(new java.awt.Color(0, 0, 204));
        jLabel8.setText("Time");
        jPanel1.add(jLabel8);
        jLabel8.setBounds(340, 40, 60, 17);

        jLabel10.setBackground(new java.awt.Color(153, 153, 255));
        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel10.setForeground(new java.awt.Color(0, 0, 204));
        jLabel10.setText("Date");
        jPanel1.add(jLabel10);
        jLabel10.setBounds(340, 70, 50, 17);

        jLabel11.setBackground(new java.awt.Color(153, 153, 255));
        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel11.setForeground(new java.awt.Color(0, 0, 204));
        jLabel11.setText(":");
        jPanel1.add(jLabel11);
        jLabel11.setBounds(120, 40, 19, 17);

        jLabel12.setBackground(new java.awt.Color(153, 153, 255));
        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel12.setForeground(new java.awt.Color(0, 0, 204));
        jLabel12.setText(":");
        jPanel1.add(jLabel12);
        jLabel12.setBounds(410, 40, 19, 17);

        jLabel13.setBackground(new java.awt.Color(153, 153, 255));
        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel13.setForeground(new java.awt.Color(0, 0, 204));
        jLabel13.setText(":");
        jPanel1.add(jLabel13);
        jLabel13.setBounds(410, 70, 19, 17);

        jLabel18.setBackground(new java.awt.Color(153, 153, 255));
        jLabel18.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel18.setForeground(new java.awt.Color(0, 0, 204));
        jLabel18.setText(":");
        jPanel1.add(jLabel18);
        jLabel18.setBounds(420, 310, 19, 17);

        jLabel19.setBackground(new java.awt.Color(153, 153, 255));
        jLabel19.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel19.setForeground(new java.awt.Color(0, 0, 204));
        jLabel19.setText(":");
        jPanel1.add(jLabel19);
        jLabel19.setBounds(120, 300, 19, 17);

        jLabel20.setBackground(new java.awt.Color(153, 153, 255));
        jLabel20.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel20.setForeground(new java.awt.Color(0, 0, 204));
        jLabel20.setText(":");
        jPanel1.add(jLabel20);
        jLabel20.setBounds(120, 330, 19, 17);

        tablebox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chose Table", "Table1", "Table2", "Table3", "Table4", "Table5", "Table6", "Table7", "Table8", "Table9" }));
        tablebox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                tableboxItemStateChanged(evt);
            }
        });
        jPanel1.add(tablebox);
        tablebox.setBounds(150, 40, 120, 20);

        date.setEditable(false);
        jPanel1.add(date);
        date.setBounds(440, 70, 125, 20);
        jPanel1.add(jTextField6);
        jTextField6.setBounds(150, 300, 102, 22);
        jPanel1.add(jTextField7);
        jTextField7.setBounds(150, 330, 102, 20);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button/PRINT.png"))); // NOI18N
        jButton2.setBorderPainted(false);
        jButton2.setContentAreaFilled(false);
        jButton2.setDefaultCapable(false);
        jButton2.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/button/PRINTMouseOver.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2);
        jButton2.setBounds(450, 350, 120, 60);

        current_time.setEditable(false);
        jPanel1.add(current_time);
        current_time.setBounds(440, 40, 125, 20);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void tableboxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_tableboxItemStateChanged
        currentDate = new Date();
        Date currentTime = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
        SimpleDateFormat formattertime = new SimpleDateFormat("kk:mm:ss");
        dateString = formatter.format(currentTime);
        String timeString = formattertime.format(currentDate);
        date.setText(dateString);
        current_time.setText(timeString);

        bug++;
        if ( bug ==2){
        getdata();
        }
    }//GEN-LAST:event_tableboxItemStateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int tableNum = tablebox.getSelectedIndex();
        try {
            Main.getConnection();
            stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

            rset = stmt.executeQuery("update order_list set Bill_status = 'P' where table_num = '"+tableNum+"' and Bill_Status = 'U' and order_date = '"+dateString+"'");
        } catch (SQLException e) {
                System.out.println("update error");
            }
        bug=-1;
        tablebox.setSelectedIndex(0);
        Print.main(null);
        jTable2.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null},
        {null, null, null, null, null}
    },
    new String [] {
        "No.", "Menu Code", "Menu Name", "Quantity", "Price"
    }
));
        CounterMenu.TableArrangement.resetTable(tableNum);

    }//GEN-LAST:event_jButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField current_time;
    private javax.swing.JTextField date;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JComboBox tablebox;
    // End of variables declaration//GEN-END:variables

}
